#ifndef PARSE2DDICOMIMAGE_H
#define PARSE2DDICOMIMAGE_H

#include <opencv2/core.hpp>
#include <QImage>
#include <QSharedPointer>
#include <vector>

struct CircleCenter {
    CircleCenter(){}
    CircleCenter(double x_, double y_, double r_) {
        x = x_;
        y = y_;
        r = r_;
    }
    double x;
    double y;
    double r;
};

struct PointDistance {
    PointDistance(){}
    PointDistance(double x_, double y_, double d_, ulong _index) {
        x = x_;
        y = y_;
        distance = d_;
        index = _index;
    }
    double x;
    double y;
    double distance;
    ulong index;
};

/// Dicom2DViewer ImageData
struct DVImage {
    QSharedPointer<QImage> qImage;  // 保存显示的图像
    cv::Mat cvMat;               // 存储8灰度图
    // TODO: 可能存放vtk读入的 dicom 原始数据图vtkImageData

    QImage *getQImageData() {
        return qImage.data();
    }
};

class Parse2DDicomImage
{
public:
    Parse2DDicomImage();
    /// 读取dicom图片, 并转成DVImage{QImage, cvMat}
    /// 2022.5.17日，采用智能指针，避免内存泄露
    static QSharedPointer<DVImage> getDicomImageData(QString& filename, bool toLog);
    /// 读取dicom图片, 并转成DVImage并通过cvMat识别小球
    /// 2022.5.17日，采用智能指针，避免内存泄露
    static QSharedPointer<DVImage> parseDicomImage(QString& filename, std::vector<cv::KeyPoint>& keyPoints,
                                   bool toLog = false, QString *imageOutputFolder = nullptr);
    static bool detectRuler2DPoints(std::vector<cv::KeyPoint>& circleCenters,
                             std::vector<cv::KeyPoint> &outerPoints,
                             std::vector<cv::KeyPoint> &innerPoints,
                             bool toLog = false, QString *imageOutputFolder = nullptr);
    /// 识别所有的点
    static bool detectIronPointInRoi(cv::Mat src, std::vector<cv::KeyPoint> &pointsVect, bool isGrayMat);
    /// 已知圆上三点。求圆心，半径
    static void calCircle(cv::Point pt1, cv::Point pt2, cv::Point pt3, CircleCenter &radius);

    /// 获取一点扩展的rect
    /// w,h 表示图像的尺寸， x，y表示选取图像中的一点， l表示取包含xy的正方形边长
    static bool getPointRect(int w, int h, int x, int y, cv::Rect &rect, int l=100);
};

#endif // PARSE2DDICOMIMAGE_H
